package com.mr.qa.taglib;

import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.mr.qa.GlobalConfigs;
import com.mr.qa.dao.impl.UserDAO;

public class UserAnswerCountTaglib extends BodyTagSupport {
	
	private String userCount; 
	
	private String categoryType;
	
	@Override
	public int doStartTag() throws JspException {
		try
        {
			Integer intCategoryType = Integer.valueOf(categoryType);
			if (intCategoryType == 0){
				userCount = "" + GlobalConfigs.MOST_ACTIVE_USER_COUNT_QUESTION;
			}else if(intCategoryType == 2){
				userCount = "" + GlobalConfigs.MOST_ACTIVE_USER_COUNT_TECH;
			}
            JspWriter out = pageContext.getOut();
            UserDAO userDAO = new UserDAO();
            List list = userDAO.getUserOrderByAnswerCount(intCategoryType, Integer.valueOf(userCount));
            out.print(makeLeft(list));
        }
        catch (Exception e)
        { 
            throw new JspException(e);
        }
    
        return this.EVAL_PAGE;
	}
	
	private String makeLeft(List list){
		StringBuffer bf = new StringBuffer();
		HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
		int i = 0;
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			i++;
			Object object = (Object) iterator.next();
			bf.append("&nbsp;&nbsp;<img src='qa/images/rank1_"+i+".gif' style='position:relative;top:3px;'/>&nbsp;&nbsp;<a href='");
			bf.append(request.getContextPath());
			bf.append("/user.mrqa?action=viewuserbyother&id=");
			bf.append(Array.get(object, 1));
			bf.append("'>");
            bf.append(Array.get(object, 2));
            bf.append("</a><br>");
		}
		return bf.toString();
	}

	public String getUserCount() {
		return userCount;
	}

	public void setUserCount(String userCount) {
		this.userCount = userCount;
	}

	public String getCategoryType() {
		return categoryType;
	}

	public void setCategoryType(String categoryType) {
		this.categoryType = categoryType;
	}
	
	
	
	
}
